//
//  UIImageView+Middleware.m
//  kunlun
//
//  Created by zhusanbao on 2020/9/24.
//  Copyright © 2020 zhusanbao. All rights reserved.
//

#import "UIImageView+Middleware.h"
#import "UIImageView+WebCache.h"
#import <Base/JYBaseStatics.h>
#import <objc/runtime.h>
#import "SDWebImageDownloader.h"

@implementation UIImageView (Middleware)

- (void)setKl_imageName:(NSString *)kl_imageName{
    self.image = U_imageNamed(kl_imageName);
}
- (NSString *)kl_imageName{return nil;}
- (void)setKl_imageUrl:(NSString *)kl_imageUrl{
    objc_setAssociatedObject(self, _cmd, kl_imageUrl, OBJC_ASSOCIATION_COPY_NONATOMIC);
    self.kl_imageUrlBlock(kl_imageUrl, nil);
}
- (NSString *)kl_imageUrl{
    return objc_getAssociatedObject(self, @selector(setKl_imageUrl:));
}
- (void)setKl_imageUrlBlock:(void (^)(NSString *, UIImage *))kl_imageUrlBlock{
    objc_setAssociatedObject(self, _cmd, kl_imageUrlBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setKl_imageUrlOptionBlock:(void (^)(NSString * _Nullable, UIImage * _Nullable, SDWebImageOptions))kl_imageUrlOptionBlock{
    objc_setAssociatedObject(self, _cmd, kl_imageUrlOptionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(NSString * _Nullable, SDWebImageOptions))kl_imageUrlOptionBlock{
    id block = objc_getAssociatedObject(self, @selector(setKl_imageUrlOptionBlock:));
    if (!block) {
        __weak typeof(self)wkSelf = self;
        block = self.kl_imageUrlOptionBlock = ^(NSString * _Nullable url, UIImage * _Nullable placeholder, SDWebImageOptions options) {
            [wkSelf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder options:options];
        };
    }
    return block;
}
- (void)setKl_imageUrlCompleteBlock:(void (^)(NSString * _Nullable, UIImage * _Nullable, SDExternalCompletionBlock _Nullable))kl_imageUrlCompleteBlock{
    objc_setAssociatedObject(self, _cmd, kl_imageUrlCompleteBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(NSString * _Nullable, UIImage * _Nullable, SDExternalCompletionBlock))kl_imageUrlCompleteBlock{
    id block = objc_getAssociatedObject(self, @selector(setKl_imageUrlCompleteBlock:));
    if (!block) {
        __weak typeof(self)wkSelf = self;
        block = self.kl_imageUrlCompleteBlock = ^(NSString * _Nullable url, UIImage * _Nullable placeholder, SDExternalCompletionBlock _Nullable complete) {
            [wkSelf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:complete];
        };
    }
    return block;
}

- (void (^)(NSString *, UIImage *))kl_imageUrlBlock{
    id block = objc_getAssociatedObject(self, @selector(setKl_imageUrlBlock:));
    if (!block) {
        __weak typeof(self)wkSelf = self;
        block = self.kl_imageUrlBlock = ^(NSString * url, UIImage * placeholder) {
            __strong typeof(wkSelf)stf = wkSelf;
#if DEBUG
            [stf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                if (imageURL && error) {
//                    sendDingTalkMessage(error.description);
                }
            }];
#else
            [stf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder];
#endif
        };
    }
    return block;
}

- (void)kl_nocacheImageUrl:(NSString *)url {
    if (url) {
        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            self.image = image;
        }];
    }else {
        self.image = nil;
    }
}

- (void)setKl_imageUrlDefaultBlock:(void (^)(NSString * _Nullable, UIImage * _Nullable, UIImage * _Nullable))kl_imageUrlDefaultBlock {
    objc_setAssociatedObject(self, _cmd, kl_imageUrlDefaultBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(NSString * _Nullable, UIImage * _Nullable, UIImage * _Nullable))kl_imageUrlDefaultBlock {
    id block = objc_getAssociatedObject(self, @selector(setKl_imageUrlDefaultBlock:));
    if (!block) {
        __weak typeof(self)wkSelf = self;
        block = self.kl_imageUrlDefaultBlock = ^(NSString *url, UIImage *placeholder, UIImage *defaultImg) {
            __strong typeof(wkSelf)stf = wkSelf;
            if (url && url.length > 0) {
                [stf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    if (error) {
                        // 失败
                        self.image = defaultImg;
                    }
                }];
            }else {
                self.image = defaultImg;
            }
        };
    }
    return block;
}

- (void)setKl_imageUrlDefaultCompleteBlock:(void (^)(NSString * _Nullable, UIImage * _Nullable, UIImage * _Nullable, SDExternalCompletionBlock _Nullable))kl_imageUrlDefaultCompleteBlock {
    objc_setAssociatedObject(self, _cmd, kl_imageUrlDefaultCompleteBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)(NSString * _Nullable, UIImage * _Nullable, UIImage * _Nullable, SDExternalCompletionBlock _Nullable))kl_imageUrlDefaultCompleteBlock {
    id block = objc_getAssociatedObject(self, @selector(setKl_imageUrlDefaultCompleteBlock:));
    if (!block) {
        __weak typeof(self)wkSelf = self;
        block = self.kl_imageUrlDefaultCompleteBlock = ^(NSString *url, UIImage *placeholder, UIImage *defaultImg, SDExternalCompletionBlock _Nullable complete) {
            __strong typeof(wkSelf)stf = wkSelf;
            if (url && url.length > 0) {
                [stf sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    if (error) {
                        // 失败
                        self.image = defaultImg;
                    }
                    if (complete) {
                        complete(image, error, cacheType, imageURL);
                    }
                }];
            }else {
                self.image = defaultImg;
            }
        };
    }
    return block;
}

@end
